import 'dart:convert' as convert;
import 'package:http/http.dart' as http;

Future<T> getRequestF<T>(url, infos) async {
  ///创建Dio对象
  var _client = http.Client();
  Map<String, dynamic>? queryP;
  int tmpindex = 1;
  infos.removeAt(0);
  Map<String, String> headersMap = {};
  print(infos);
  if (infos != null) {
    for (int i = 0; i < infos.length; i++) {
      print("读取数据: ${infos[i]}");
      if (infos[i] != "" && infos[i] != "\r") {
        var kword = infos[i].split(":");
        if (kword.length == 2) {
          String k = kword[0];
          String v = kword[1];
          headersMap[k] = v;
        } else {
          tmpindex = i + 1;
          break;
        }
      } else {
        tmpindex = i + 1;
        break;
      }
    }
    var infos2;
    if (tmpindex < infos.length) {
      infos2 = infos.getRange(tmpindex, infos.length);
    } else {
      infos2 = null;
    }
    if (infos2 != null) {
      String infos3 = infos2.join(" ");
      print("我到这里: $infos2");
      queryP = convert.jsonDecode(infos3);
      print("合并后的json: $queryP");
      queryP = null;
    }
  } else {
    queryP = null;
  }

  ///发起get请求
  print("请求url：$url");
  http.Response? response;
  String? msg;
  try {
    response = await _client.get(Uri.parse(url), headers: headersMap);
    print("http： ${response.body.toString()}");
  } on Exception catch (e) {
    msg = e.toString();
  }
  T data;

  ///响应数据

  if (msg != null) {
    data = msg as T;
  } else {
    data = response as T;
  }
  return data;
}

Future<T> postRequestF<T>(url, infos) async {
  ///创建Dio对象
  var _client = http.Client();
  String? queryP;
  int tmpindex = 1;
  infos.removeAt(0);
  Map<String, String> headersMap = {};
  print("收到infos: $infos");
  if (infos != null) {
    for (int i = 0; i < infos.length; i++) {
      print("读取数据: ${infos[i]}");
      if (infos[i] != "" && infos[i] != "\r") {
        var kword = infos[i].split(":");
        if (kword.length == 2) {
          String k = kword[0];
          String v = kword[1];
          headersMap[k] = v;
        } else {
          tmpindex = i + 1;
          break;
        }
      } else {
        tmpindex = i + 1;
        break;
      }
    }
    var infos2;
    if (tmpindex < infos.length && infos[tmpindex] != "") {
      infos2 = infos.getRange(tmpindex, infos.length);
    } else {
      infos2 = null;
    }
    if (infos2 != null) {
      String infos3 = infos2.join(" ");
      print("我到这里: $infos2");
      var querypd = convert.jsonDecode(infos3);
      queryP = convert.json.encode(querypd);
      print("合并后的json: $queryP");
    }
  } else {
    queryP = null;
  }

  ///发起post请求
  print("请求url：$url");
  http.Response? response;
  String? msg;
  try {
    print("请求body: $queryP");
    response =
        await _client.post(Uri.parse(url), headers: headersMap, body: queryP);
    print("http： ${response.body.toString()}");
  } on Exception catch (e) {
    msg = e.toString();
  }

  T data;

  ///响应数据
  if (msg != null) {
    data = msg as T;
  } else {
    data = response as T;
  }
  return data;
}
